package com.boot.mall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.boot.mall.product.entity.ProductAttrValueEntity;
import com.boot.mall.product.service.ProductAttrValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.boot.mall.product.entity.AttrEntity;
import com.boot.mall.product.service.AttrService;
import com.boot.common.utils.PageUtils;
import com.boot.common.utils.R;



/**
 * 商品属性
 *
 * @author huihui
 * @email 2565036109@qq.com
 * @date 2021-05-06 14:41:26
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;
    @Autowired
    private ProductAttrValueService productAttrValueService;

    // 查询spu的规格参数
    @GetMapping("/base/listforspu/{spuId}")
    public R baseAttrListForSpu(@PathVariable("spuId") Long spuId){
        List<ProductAttrValueEntity> attrValueEntities = productAttrValueService.listAttrForSpu(spuId);
        return R.ok().put("data",attrValueEntities);
    }

    // 修改spu的规格参数
    @PostMapping("/update/{spuId}")
    public R updateSpuAttr(@PathVariable("spuId") Long spuId,@RequestBody List<ProductAttrValueEntity> entities){
        productAttrValueService.updateSpuAttr(spuId,entities);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        return R.ok().put("page", "");
    }

    /**
     * 根据分类id分页查询   销售属性
     */
    @GetMapping("/sale/list/{categoryId}")
    public R getSaleAttrList(@RequestParam Map<String,Object> params,@PathVariable("categoryId") String categoryId){
        PageUtils pageUtils = attrService.pageQueryByCategoryId(params,categoryId);
        return R.ok().put("page",pageUtils);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    public R info(@PathVariable("attrId") Long attrId){
		AttrEntity attr = attrService.getById(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrEntity attr){
		attrService.save(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrEntity attr){
		attrService.updateById(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
